<template>
  <div class="good">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <div style="margin-top: 15px;">
      <el-input placeholder="请输入内容" style="width:300px;margin-right:15px">
        <el-button slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <el-button type="success" @click="$router.push({name: 'addGood'})">添加商品</el-button>
    </div>
    <!-- 表格展示 -->
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="300px"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="100px"></el-table-column>
        <el-table-column prop="goods_state" label="商品状态" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column prop="add_time " label="创建时间" width="150px">
            <template slot-scope='scope'>
              {{scope.row.add_time | timeFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <el-button type="primary" icon="el-icon-edit"></el-button>
          <el-button type="primary" icon="el-icon-share"></el-button>
          <el-button type="primary" icon="el-icon-delete"></el-button>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页导航 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { getGoodlist } from '@/api/goodList-index.js'
import { timeFormat } from '@/utils/myfiltes.js'
export default {
  data () {
    return {
      // 表格展示数据
      tableData: [],
      total: 0,
      params: {
        query: '',
        pagenum: 1,
        pagesize: 20
      }
    }
  },
  //   注册一个时间过滤器
  filters: {
    timeFormat
  },
  methods: {
    //   获取商品列表
    init () {
      getGoodlist(this.params)
        .then(res => {
        //   console.log(res)
          if (res.data.meta.status === 200) {
            this.tableData = res.data.data.goods
            this.total = res.data.data.total
          }
        })
    },
    handleSizeChange (val) {
    //   console.log(`每页 ${val} 条`)
      this.params.pagesize = val
      this.init()
    },
    handleCurrentChange (val) {
    //   console.log(`当前页: ${val}`)
      this.params.pagenum = val
      this.init()
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style lang="less">
</style>
